package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.stadium.api.req.ReqId;
import com.quanyan.stadium.api.resp.RespUserGradeIntegral;
import com.quanyan.stadium.api.resp.RespUserGradeIntegralList;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.vo.GradeIntegralVo;
import com.quanyan.stadium.service.GradeIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongshenglin
 * @date 2016/6/30  12:01
 */
@RestController
@RequestMapping("/api/stadium/gradeIntegral")
public class GradeIntegralController {

    @Autowired
    private GradeIntegralService gradeIntegralService;

    @RequestMapping(value = "/getGradeIntegralExplain",method = RequestMethod.POST)
    public APIResponse<List<GradeIntegralVo>> getGradeIntegralExplain(@RequestBody ReqId req){
        List<Integer> categoryIds = new ArrayList<>();
        if(null != req.getCategoryId()){
            categoryIds.add(req.getCategoryId());
        }else{
            categoryIds.add(GameConstants.CATEGORY_ID_TENNIS);
            categoryIds.add(GameConstants.CATEGORY_ID_BADMINTON);
        }
        return APIResponse.returnSuccess(gradeIntegralService.getGradeIntegralExplain(categoryIds));
    }

    @RequestMapping(value = "/getUserGradeIntegral",method = RequestMethod.POST)
    public APIResponse<RespUserGradeIntegralList> getUserGradeIntegral(@RequestBody ReqId req,HttpServletRequest request){
        Integer uid = (Integer)request.getAttribute("uid");
        List<Integer> categoryIds = new ArrayList<>();
        if(null != req.getCategoryId()){
            categoryIds.add(req.getCategoryId());
        }else{
            categoryIds.add(GameConstants.CATEGORY_ID_TENNIS);
            categoryIds.add(GameConstants.CATEGORY_ID_BADMINTON);
        }
        return APIResponse.returnSuccess(gradeIntegralService.getUserGradeIntegral(uid,categoryIds));
    }

    @RequestMapping(value = "/evalOwnFLScore",method = RequestMethod.POST)
    public APIResponse evalOwnFLScore(@RequestBody ReqId req,HttpServletRequest request){
        Integer uid = (Integer)request.getAttribute("uid");
        if(null == req.getCategoryId() || null == req.getScore()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        try {
            gradeIntegralService.evalOwnFLScore(uid,req.getScore(),req.getCategoryId());
        }catch (Exception e){
            return APIResponse.returnFail("");
        }
        return APIResponse.returnSuccess();
    }
}
